/**
 * Created by Administrator on 2018/5/4.
 */
define(function (require,exports,module) {
    require("./room.css");
    var sTpl = require("./template.html");
    var VueComponent = Vue.extend({
        props:['parentData'],
        template: sTpl,
        methods:{
          edit:function(scope){
              scope.edit=true;
          },
            chose:function (room,index) {
              if(room.roomStatus==1&&this.parentData!=null){
                  this.chooseRoomId=room.roomId;
                  this.parentData.selectFunction(room);
              }
            },
            cancleEdit:function (scope,cancleUpdate) {
              if(cancleUpdate){
                  this.$refs[scope.roomId][0].resetFields();
              }
                scope.edit=false;
            },searchAll:function(){
              var $this = this;
                axios.get(basepath+'/api/room/queryRoom?filter='+this.filter)
                    .then(function (response) {
                        response.data.forEach(function (data,i) {
                            data.edit=false;
                        })
                        $this.rooms=response.data;
                    });
            },deleteRoom:function (scope,index) {
                var $this=this;
                this.$confirm('删除后将无法恢复', '确定删除该房间？', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'success'
                }).then(function() {
                    axios.post(basepath+'/api/room/deleteRoom',scope)
                        .then(function (response) {
                            $this.rooms.splice(index,1);
                            $this.$notify({
                                title: '删除成功',
                                message: '房间已删除'
                            });
                        });
                })
            }

            ,addRooms:function () {
              var $this=this;
               this.$refs.addRoom.validate(function(validate){
                 if(validate){
                     axios.post(basepath+'/api/room/saveRoom',$this.addRoom)
                         .then(function (response) {
                             $this.rooms.unshift(response.data);
                             $this.$refs["addRoom"].resetFields();
                             $this.addRoom.edit=false;
                             $this.$notify({
                                 title: '添加成功',
                                 message: '添加成功'
                             });
                         });
                 }
               });
            },
            update:function (scope,validate) {
                var $this=this;
                if(validate){
                    this.$refs[scope.roomId][0].validate(function(validate){
                        if(validate){
                            axios.post(basepath+'/api/room/saveRoom',scope)
                                .then(function (response) {
                                    scope.edit=false;
                                    $this.$notify({
                                        title: '更新成功',
                                        message: '更新成功'
                                    });
                                });
                        }
                    });
                }else{
                    axios.post(basepath+'/api/room/saveRoom',scope)
                        .then(function (response) {

                            $this.$notify({
                                title: '房间状态更新成功',
                                message: '房间状态更新成功'
                            });
                        });
                }

            }
        },
        data:function () {
           return {
               addRoom: {
                   roomName: '',
                   roomNum: '',
                   roomStatus: "true",
                   roomPosition: "",
                   edit:false
               },
               rooms:[ {
                   roomName: '月桂坊',
                   roomNum: '101',
                   roomStatus: "1",
                   roomPosition: "1楼左转",
                   edit:false
               }],
               chooseRoomId:''
               ,
               filter:"",
               cardWidth: 500,
               pointer:false
           };
        },
        created:function (){
            this.searchAll();
        },
        activated:function () {
            if(this.parentData){
                this.pointer=true;
                if(this.parentData.selectRoom){
                    this.chooseRoomId=this.parentData.selectRoom.roomId;
                }

            }else{
                messages.$emit('change','房间录入');
            }
        }

    });
    module.exports = VueComponent;
    return VueComponent;

})